<?php
$all_types[] = array ('text' => '--None--',	'value' => '0');
$all_types[] = array ('text' => 'Text Box', 'value' => 'text');
$all_types[] = array ('text' => 'Number Box', 'value' => 'text_num');
$all_types[] = array ('text' => 'Email Box', 'value' => 'text_email');
$all_types[] = array ('text' => 'Textarea', 'value' => 'textarea');
$all_types[] = array ('text' => 'Editor', 'value' => 'editor');
$all_types[] = array ('text' => 'Select Box', 'value' => 'select');
$all_types[] = array ('text' => 'Select Multiple', 'value' => 'select_multiple');
$all_types[] = array ('text' => 'Check Boxes', 'value' => 'checkbox');
$all_types[] = array ('text' => 'Date', 'value' => 'date');
$all_types[] = array ('text' => 'Date Added', 'value' => 'date_added');
$all_types[] = array ('text' => 'Date Modified', 'value' => 'date_modified');
$all_types[] = array ('text' => 'Date Time', 'value' => 'datetime');
$all_types[] = array ('text' => 'Status', 'value' => 'status');

$all_options[] = array ('text' => ' &nbsp; List &nbsp;  ', 'value' => 'list');
$all_options[] = array ('text' => ' &nbsp; Sort &nbsp; ', 'value' => 'sort');
$all_options[] = array ('text' => 'Search', 'value' => 'search');
$all_options[] = array ('text' => 'Form', 'value' => 'form');
$all_options[] = array ('text' => 'Req *', 'value' => 'require');

$other_options[] = array ('text' => 'Catalog Controller', 'value' => 'catalog_controller');
$other_options[] = array ('text' => 'Admin Controller', 'value' => 'admin_controller');
$other_options[] = array ('text' => 'Catalog Model', 'value' => 'catalog_model');
$other_options[] = array ('text' => 'Admin Model', 'value' => 'admin_model');
$other_options[] = array ('text' => 'Catalog Language', 'value' => 'catalog_language');
$other_options[] = array ('text' => 'Admin Language', 'value' => 'admin_language');
$other_options[] = array ('text' => 'Catalog View', 'value' => 'catalog_list');
$other_options[] = array ('text' => 'Admin View', 'value' => 'admin_list');
$other_options[] = array ('text' => 'Catalog Form', 'value' => 'catalog_form');
$other_options[] = array ('text' => 'Admin Form', 'value' => 'admin_form');


$other_options[] = array ('text' => '', 'value' => '');
$other_options[] = array ('text' => '', 'value' => '');
$other_options[] = array ('text' => 'Safe Delete', 'value' => 'safe_delete');
$other_options[] = array ('text' => 'Debug Mode', 'value' => 'debug_mode');


function filter(& $item, $key) {
	$item = trim($item);
}

function make_name($name) {
	$name = str_replace('_', ' ', $name);
	$name = ucwords($name);
	return $name;
}

function get_fields_names($type, $all_fields) {
	
	$all_fields = get_fields($type, $all_fields);
	$all_fields = get_names('name', $all_fields);
	
	return $all_fields;
}

function get_list_order($module_fields) {
	$order = $_POST['order_list'];
	asort($order);
	$module_fields_name = get_names('name', $module_fields);
	$list_fields = get_fields('list', $module_fields);
	$list_fields_name = get_names('name', $list_fields);
	
	$sorted_fields = array();
	foreach($list_fields_name as $key => $each_name) {
		$key = array_search($each_name, $module_fields_name);		
		$sorted_fields[$key] = $each_name;
	}
	
	$list_fields_name = $sorted_fields;
	$all_keys = array_keys($list_fields_name);
	
	foreach($order as $key => $each_order) {
		if(! in_array($key, $all_keys))
			unset($order[$key]);
	}
	
	$sorted_fields = array();
	foreach($order as $key => $each_order) {
		$sorted_fields[] = $module_fields[$key];
	}
			
	return $sorted_fields;
}

function get_form_order($module_fields) {
	$order = $_POST['order_form'];
	asort($order);
	$module_fields_name = get_names('name', $module_fields);
	$form_fields = get_fields('form', $module_fields);
	$form_fields_name = get_names('name', $form_fields);
	
	$sorted_fields = array();
	foreach($form_fields_name as $key => $each_name) {
		$key = array_search($each_name, $module_fields_name);		
		$sorted_fields[$key] = $each_name;
	}
	
	$form_fields_name = $sorted_fields;	
	$all_keys = array_keys($form_fields_name);
	
	foreach($order as $key => $each_order) {
		if(! in_array($key, $all_keys))
			unset($order[$key]);
	}
	
	$sorted_fields = array();
	foreach($order as $key => $each_order) {
		$sorted_fields[] = $module_fields[$key];
	}
			
	return $sorted_fields;
}

function sort_fields($data=array(), $order=array()) {
	foreach($order as $key => $value) {
		
		if(isset($data[$value]))
			$order[$key] = $data[$value];
		else
		 	unset($order[$key]);
	}
		
	return $order;
}

//function sort_this(&$item, $key, $data=array()) {
//	echo $item." | ".$key;
//	print_r($data);
//	$key = $data[$key];
//	 $item = $data[$item];	
//}

function minus_one(&$item, $key) {	
	 $item = $item - 1;
}

function get_names($type, $all_fields) {
	$filter_fields = array();
	
	foreach($all_fields as $each_field) {
		$filter_fields[] = $each_field[$type];
	}
	
	return $filter_fields;
}

function get_fields($type, $all_fields) {
	
	$filter_fields = array();
	
	foreach($all_fields as $each_field) {
		
		if(isset($each_field['options']))
			$option_arr = $each_field['options'];
		else
		 	$option_arr = array();
		
		if(in_array($type, $option_arr))
			$filter_fields[] = $each_field;
	}
	
	return $filter_fields;
}

function match_type($type, $name) {
	switch ($type) {
		case 'DATETIME' :
		case 'TIMESTAMP' :
			if(preg_match('/modified/', $name))
				return 'date_modified';
			else if(preg_match('/added/', $name))
				return 'date_added';
			else
				return 'date';				
			break;
		case 'DATE' :
		case 'TIME' :
		case 'YEAR' :
			return 'datetime';
			break;
		case 'BLOB' :
		case 'TINYBLOB' :
		case 'BINARY' :
		case 'VARBINARY' :
		case 'MEDIUMBLOB' :
		case 'TEXT' :
		case 'MEDIUMTEXT' :
		case 'LONGBLOB' :
		case 'LONGTEXT' :
			return 'textarea';
			break;
		case 'ENUM' :
		case 'SET' :
		case 'BIT' :
		case 'BOOL' :
			return 'status';
			break;
		default :
			if(preg_match('/id/', $name) || preg_match('/num/', $name))
				return 'text_num';
			else if(preg_match('/email/', $name))
				return 'text_email';
			else
				return 'text';
			break;
	}
}
